package com.atguigu.gmall.mq.listener;

import com.atguigu.gmall.mq.config.DelayedMqConfig;
import com.atguigu.gmall.mq.config.MQConfig;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.impl.AMQImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;

/**
 * @Author: lx
 * @Time: 10:39
 * @Description:
 *
 *  消息者
 */
@Component
@Slf4j
public class CustemerListener {


    //延迟插件
/*    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(name = MQConfig.queue_delay_1,durable = "true"
                            ,autoDelete = "false"),
                    exchange = @Exchange(value = MQConfig.exchange_delay,
                            type = "x-delayed-message",delayed = "true",
                            arguments = {@Argument(name = "x-delayed-type",value = "direct")}),//延迟交换机
                    key = {MQConfig.routing_delay}
            )
    })*/
    @RabbitListener(queues = DelayedMqConfig.queue_delay_1)
    public void receverMessage2(String msg, Channel channel,Message message){
        try {
            System.out.println(new Date() + ":" + msg);
            channel.basicAck(message.getMessageProperties().getDeliveryTag()
                    ,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //死信
    @RabbitListener(queues = MQConfig.queue_dead_2)
    public void receverMessage1(String msg,Channel channel,Message message){
        try {
            System.out.println(new Date() + ":" + msg);
            channel.basicAck(message.getMessageProperties().getDeliveryTag()
                    ,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    //接收消息
    @RabbitListener(queues = "queue1")
    public void receiverMessage(String msg, Message message, Channel channel) throws IOException {
        System.out.println(message);

        try {
            //30行代码
            //应答
            int i = 1/0;
            //参数1： 消息的标记
            //参数2：累计确认
            // true: 从现在开始计算 将之前确认过未确认的消息 都确认
            // false: 只确认当前

            channel.basicAck(message.getMessageProperties().getDeliveryTag()
                      ,false);
        } catch (Exception e) {
           // e.printStackTrace();
            //判断消息是否为重新发送
            if(message.getMessageProperties().isRedelivered()){
                //已经是重发的消息  不让再发了
                log.error(new Date() + ":已经是重发的消息  不让再发了：" + msg);
                //参数1： 消息的标记
                //参数2：是否放回队列   true 表示放回队列  false : 不放回
                channel.basicReject(message.getMessageProperties().getDeliveryTag()
                        ,false);//不入队
            }else{
                //是新的消息   可以重发一次
                log.error(new Date() + ":新的消息   可以重发一次"); //不要再给我消息了
                //参数1： 消息的标记
                //参数2：是否批量
                //参数3：是否放回队列   true 表示放回队列  false : 不放回
                channel.basicNack(message.getMessageProperties().getDeliveryTag()
                ,false,true);
            }
        }


    }
}
